package com.example.onlinemonitoring.comCenter;

import com.example.onlinemonitoring.Util.CRC16;
import com.example.onlinemonitoring.Util.CommPortManager;
import com.example.onlinemonitoring.Util.IEEE754Utils;
import com.example.onlinemonitoring.model.Appdevice;
import com.example.onlinemonitoring.model.Sf6sensor;
import com.example.onlinemonitoring.serviceImpl.Sf6sensorServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

@Service("sf661850Com")
@Scope("prototype")
public class Sf661850Com extends CommPortManager {
    @Autowired
    private Sf6sensorServiceImpl sf6serviceimpl;

    @Override
    public void receiveData(String str) {
        setText("收到sanway请求：" + str + "\r\n");
        boolean flag233 = str.length() < 16;
        String s = "";
        if (!flag233) {
            int deviceaddr = parseHex4(str.substring(0, 2));
            boolean flag234 = str.substring(2, 4).equals("04");
            if (flag234) {
                Sf6sensor sf6 = new Sf6sensor();
                sf6.setStationid(ad.getStationid());
                sf6.setDevicegroup(ad.getDevicegroup());
                sf6.setDeviceaddr(deviceaddr);
                sf6 = sf6serviceimpl.getModel(sf6);
                if (sf6 != null) {
                    boolean flag235 = str.substring(4, 6).equals("10");
                    if (flag235) {
                        s += String.format("%02x", deviceaddr);
                        s += "0418";
                        s += IEEE754Utils.singleToHex(nto(sf6.getYalijz()));
                        s += IEEE754Utils.singleToHex(nto(sf6.getYalijzpy()));
                        s += IEEE754Utils.singleToHex(nto(sf6.getWdjz()));
                        s += IEEE754Utils.singleToHex(nto(sf6.getWdjzpy()));
                        s += IEEE754Utils.singleToHex(nto(sf6.getSdjz()));
                        s += IEEE754Utils.singleToHex(nto(sf6.getSdjzpy()));
                        s += CRC16.calcCrc16(s);
                        Send(s);
                        setText("SANWAY模块转发微水修正值：" + s + "\r\n");
                    }
                    boolean flag237 = str.substring(4, 6).equals("00");
                    if (flag237) {
                        s += String.format("%02x", deviceaddr);
                        s += "041C";
                        s += IEEE754Utils.singleToHex(nto(sf6.getLastpressure()));// 压力
                        s += IEEE754Utils.singleToHex(nto(sf6.getLasttemperature()));// 温度
                        s += IEEE754Utils.singleToHex(nto(sf6.getLasthumidity()));// 相对湿度
                        s += IEEE754Utils.singleToHex(nto(sf6.getLastdensity()));// 密度
                        s += IEEE754Utils.singleToHex(nto(sf6.getLastdaiya()));// 水汽分压
                        s += IEEE754Utils.singleToHex(nto(sf6.getLastchangya()));// 露点
                        s += IEEE754Utils.singleToHex(nto(sf6.getLastppm()));// 微水
                        s += CRC16.calcCrc16(s);
                        Send(s);
                        setText("SANWAY模块转发微水数据值：" + s + "\r\n");
                    }
                }
            }
        }
    }
}
